package com.bdjk.bdmw.InfoGath.communication.server.tcp.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bdjk.bdmw.InfoGath.common.constants.ChannelMap;

public class SendClientCommand{
  Logger logger=Logger.getLogger(SendClientCommand.class);
  Map<String, Channel> map = ChannelMap.channelMap;
  public int send(String terId, String msg){
	  
	  Channel channel = (Channel)this.map.get(terId);
	  if (null != channel&&channel.isWritable()) {
		  logger.info(msg);
		  ByteBuf encoded = channel.alloc().buffer(4 * msg.length());
		  encoded.writeBytes(msg.getBytes());
		  channel.writeAndFlush(encoded);
		  return 0;
		
	  } else {
		  logger.info("server---------"+msg+" can't be executed since the channel is bad.");
		  return -1;
	  }
  }
  
}
